package com.yituo.mjworld.mvc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yituo.mjworld.core.exception.MyException;
import com.yituo.mjworld.core.web.core.BaseForm;
import com.yituo.mjworld.core.web.core.Result;
import com.yituo.mjworld.mvc.dao.ParentItemDao;
import com.yituo.mjworld.mvc.entity.web.WebParentItem;
import com.yituo.mjworld.mvc.form.DeleteForm;
import com.yituo.mjworld.mvc.form.QueryForm;
import com.yituo.mjworld.mvc.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class ParentItemServiceImpl extends BaseService {

  @Autowired
  ParentItemDao parentItemDao;


  /**
   * 查询的以及分类
   *
   * @param form
   * @return
   */
  @Override
  public JSONObject getList(QueryForm form) {
    System.out.println(form);
    String countSql = "select count(id) from web_parent_item";
    String sql = "select id,title,orderNum from web_parent_item order by orderNum ";
    return this.get(sql, countSql, form, null);
  }

  /**
   * 删除父分类
   *
   * @param form 欲删除的父级别分类ID
   * @return
   */
  @Override
  public Result delete(DeleteForm form) {
    Result result = new Result();
    String sql = "select count(id) from web_child_item where parentId=:parentId";
    Map<String, Object> param = new HashMap<String, Object>();
    param.put("parentId", form.getId());
    long childNum = baseDao.countBySql(sql, param);
    if (childNum == 0) {
      baseDao.delete(WebParentItem.class, form.getId());
      result.result = true;
    } else {
      result.setResult(false).setMsg("您删除的分类下还有详细的子分类,请先删除子分类后再删除父分类!");
    }
    return result;
  }

  /**
   * 新增或更新 父级Item
   *
   * @param form 欲操作的父级item
   * @return Result
   */
  @Override
  public Result saveOrUpdate(BaseForm form) {
    Result result = new Result();
    WebParentItem item = (WebParentItem) form;
    WebParentItem item1 = (WebParentItem) baseDao.get(WebParentItem.class, item.getId());
    WebParentItem item2 = parentItemDao.getParentItemByTitle(item.getTitle());
    if (item1 == null) {
      // 新增操作
      if (item2 == null) {
        // 不存在重复的标题
        baseDao.saveOrUpdate(item);
        result.setResult(true);
      } else {
        result.setMsg("重复的标题").setResult(false);
      }
    } else {
      // update操作
      if (item2 == null) {
        // 不存在重复标题
        baseDao.merge(item);
        result.setResult(true);
      } else {
        // 存在重复的标题
        if (item.getId() == item2.getId()) {
          baseDao.merge(item);
          // 内容无修改
          result.setResult(true);
        } else {
          result.setResult(false).setMsg("重复的标题");
        }
      }
    }
    return result;
  }

  /**
   * 前端回传的排序用的字符串
   *
   * @param sort
   * @return
   */
  public boolean sort(String sort) throws MyException {
    String[] arr = sort.split(";");
    String sql;
    String[] arr2;
    for (String e : arr) {
      arr2 = e.replace("[", "").replace("]", "").split(",");
      int id = Integer.parseInt(arr2[0]);
      String title = arr2[1];
      int orderNum = Integer.parseInt(arr2[2]);
      if (id <= 0) {
        throw new MyException("ID不能小于0!");
      }
      if (StringUtils.isEmpty(title)) {
        throw new MyException("标题不能为空!");
      }
      WebParentItem item1 = (WebParentItem) baseDao.get(WebParentItem.class, id);
      WebParentItem item2 = this.parentItemDao.getParentItemByTitle(title);
      if (item1 == null) {
        throw new MyException("ID:" + arr2[0] + "不存在!");
      }
      if (item2 == null) {
        // 修改后的标题不重复
        if (this.parentItemDao.update(id, title, orderNum) != 1) {
          throw new MyException("未知的异常!");
        }
      } else {
        // 标题重复
        if (id == item2.getId()) {
          // 两ID相等
          if (this.parentItemDao.update(id, title, orderNum) != 1) {
            throw new MyException("未知的异常!");
          }
        } else {
          // 两ID不等
          throw new MyException("包含重复标题!");
        }
      }
    }
    return true;
  }
}
